package com.example.wifimonitor;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class WifiLog {
    private static final int MAX_LOGS = 50;
    private static WifiLogDbHelper dbHelper;
    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());

    public static synchronized void initialize(Context context) {
        if (dbHelper == null) {
            dbHelper = new WifiLogDbHelper(context.getApplicationContext());
        }
    }

    private static synchronized String getLatestLogEvent() {
        if (dbHelper == null) return null;

        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(WifiLogDbHelper.TABLE_LOGS,
                new String[]{WifiLogDbHelper.COLUMN_EVENT},
                null, null, null, null, WifiLogDbHelper.COLUMN_TIMESTAMP + " DESC", "1");

        String latestEvent = null;
        if (cursor.moveToFirst()) {
            latestEvent = cursor.getString(cursor.getColumnIndexOrThrow(WifiLogDbHelper.COLUMN_EVENT));
        }
        cursor.close();
        // db.close(); // Keep db open for subsequent operations in addLog if any, or close if this is the final op for this db instance
        return latestEvent;
    }

    public static synchronized void addLog(String event) {
        if (dbHelper == null) return; // Ensure dbHelper is initialized

        String latestEvent = getLatestLogEvent();
        // If the latest event is the same as the current event, don't add a new log.
        if (event.equals(latestEvent)) {
            return;
        }

        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String timestamp = dateFormat.format(new Date());

        ContentValues values = new ContentValues();
        values.put(WifiLogDbHelper.COLUMN_TIMESTAMP, timestamp);
        values.put(WifiLogDbHelper.COLUMN_EVENT, event);

        db.insert(WifiLogDbHelper.TABLE_LOGS, null, values);

        // Keep only the latest MAX_LOGS entries
        Cursor cursor = db.query(WifiLogDbHelper.TABLE_LOGS, new String[]{WifiLogDbHelper.COLUMN_ID}, null, null, null, null, WifiLogDbHelper.COLUMN_ID + " ASC");
        if (cursor.getCount() > MAX_LOGS) {
            cursor.moveToFirst();
            long oldestId = cursor.getLong(cursor.getColumnIndexOrThrow(WifiLogDbHelper.COLUMN_ID));
            db.delete(WifiLogDbHelper.TABLE_LOGS, WifiLogDbHelper.COLUMN_ID + " = ?", new String[]{String.valueOf(oldestId)});
        }
        cursor.close();
        db.close();
    }

    public static synchronized List<String> getLogs() {
        if (dbHelper == null) return new ArrayList<>(); // Ensure dbHelper is initialized

        List<String> logs = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(WifiLogDbHelper.TABLE_LOGS, 
                                 new String[]{WifiLogDbHelper.COLUMN_TIMESTAMP, WifiLogDbHelper.COLUMN_EVENT}, 
                                 null, null, null, null, WifiLogDbHelper.COLUMN_TIMESTAMP + " DESC");

        if (cursor.moveToFirst()) {
            do {
                String timestamp = cursor.getString(cursor.getColumnIndexOrThrow(WifiLogDbHelper.COLUMN_TIMESTAMP));
                String event = cursor.getString(cursor.getColumnIndexOrThrow(WifiLogDbHelper.COLUMN_EVENT));
                logs.add(timestamp + " - " + event);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return logs;
    }

    public static synchronized void clearLogs() {
        if (dbHelper == null) return; // Ensure dbHelper is initialized

        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(WifiLogDbHelper.TABLE_LOGS, null, null);
        db.close();
    }
}